//
//  InvateLinkView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/16.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class InvateLinkView: NSView {
    
    var touchAction:os_block_t?
    var current:DSAppearance = .dark
    
    var mainColor:NSColor {
        return self.current == .dark ? color_gray(230):color_gray(40)
    }
    
    var backColor:NSColor {
        return self.current == .dark ? color_gray(30):color_gray(230)
    }
    
    var backHoverColor:NSColor {
        return self.current == .dark ? color_gray(120):color_gray(220)
    }
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    convenience init(appearance:DSAppearance,touchAction:os_block_t?) {
        self.init()
        self.current = appearance
        self.touchAction = touchAction
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
        }
        
        self.addSubview(copyIcon)
        copyIcon.snp.makeConstraints { (make) in
            make.width.equalTo(76)
            make.height.equalTo(26)
            make.top.equalTo(textField.snp.bottom).offset(5)
            make.left.equalToSuperview().offset(0)
        }
    }
    
    lazy var textField: NSTextField = {
        let lab = NSTextField.init(string: "https://www.1meeting.cn/articledetail?aid=940")
        lab.backgroundColor = NSColor.clear
        lab.textColor = mainColor
        lab.maximumNumberOfLines = 0
        lab.preferredMaxLayoutWidth = 300-80
        lab.maximumNumberOfLines = 0
        lab.cell?.usesSingleLineMode = false
        lab.cell?.wraps = true
        lab.cell?.lineBreakMode = .byCharWrapping
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var copyIcon: KIcon = {
        let btn = KIcon.init(data: KIconGroup.init(title: "复制信息", image: "icon_invate_copy"))
        btn.imagePosition = .left
        btn.title.textColor = color_main140
        btn.bgView.layer?.backgroundColor = self.backColor.cgColor
        btn.isHidden = true
        btn.tap = { _ in
            if self.touchAction != nil {
                self.touchAction!()
            }
        }
        btn.status = { view in
            switch view {
            case .normal,.selected:
                btn.bgView.layer?.backgroundColor = self.backColor.cgColor
            case .hover,.highlight:
                btn.bgView.layer?.backgroundColor = self.backHoverColor.cgColor
            }
        }
        btn.wantsLayer = true
        btn.layer?.cornerRadius = 3
        btn.layer?.masksToBounds = true
        return btn
    }()
    
}
extension InvateLinkView:NSTextFieldDelegate {
    override func performKeyEquivalent(with event: NSEvent) -> Bool {
        
        return self.textField.performKeyEquivalent(with: event)
    }
}
